package com.k.kpp.util;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;

public class XmlW3cUtil {
    public static void main(String[] args) {
        parseXml2();
    }

    public static void parserXml() {
        try {
            String xmlPath = "c:/test/a.xml";
            String xmlContent = "";
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            //Document document = db.parse(new InputSource(new StringReader(xmlContent)));
            Document document = db.parse(xmlPath);
            Element root = document.getDocumentElement();
            NodeList bodyNode =  root.getChildNodes();
            System.out.println(bodyNode.item(0).getNodeName());
            System.out.println(bodyNode.item(1).getNodeName());
            System.out.println(bodyNode.item(2).getNodeName());
            /*HashMap<String,String> hash=new HashMap<String,String>();*/
            for (int i = 0; i < bodyNode.getLength(); i++) {

                Node responseNode = bodyNode.item(i);
                if(responseNode instanceof  Element){
                    System.out.println("i"+i);
                    NodeList responseNodeChildNodes = responseNode.getChildNodes();
                    for (int j = 0; j < responseNodeChildNodes.getLength(); j++) {
                        Node dataWrap = responseNodeChildNodes.item(j);
                        if(dataWrap instanceof Element){
                            NodeList dataWrapChildNodes = dataWrap.getChildNodes();
                            for (int k = 0; k < dataWrapChildNodes.getLength(); k++) {
                                Node itm = dataWrapChildNodes.item(k);
                                if(itm instanceof Element){
                                    Element itmEle = (Element) itm;
                                    String itmK = itmEle.getTagName();
                                    String itmV = itmEle.getTextContent();
                                    System.out.println(itmK+":"+itmV);
                                }
                                //
                                // hash.put(sessionMeta.item(k).getNodeName(), sessionMeta.item(k).getTextContent());
                            }
                        }
                    }
                }

            }
            System.out.println("解析完毕");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
           e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void parseXml2(){
        String fp = "c:/test/a.xml";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db;
        try {
            db = dbf.newDocumentBuilder();
            Document doc = db.parse(fp);
            Element root = doc.getDocumentElement();
            System.out.println(root.toString());//[soap:Envelope: null]

            Element ele = (Element) selectSingleNode("/Envelope/Body/getOneResponse", root);
            NodeList nodeList = ele.getChildNodes();
            for(int i=0;i<nodeList.getLength();i++){
                Node node = nodeList.item(i);
                short nodeType = node.getNodeType();
                System.out.println("---:"+node.getNodeName()+":"+node.getNodeType()+":"+node.getTextContent()+"childLength:"+node.getChildNodes().getLength());
                if(nodeType==1){
                    // do
                    Element myele = (Element) node;
                    boolean returnHasChilds = myele.hasChildNodes();
                    System.out.println("returnHasChilds:"+returnHasChilds);

                    String wrapKey = node.getNodeName();
                    System.out.println("key:"+wrapKey+" value:"+node.getTextContent());
                    NodeList wrapChilds = node.getChildNodes();
                    for(int j=0;j<wrapChilds.getLength();j++){
                        Node cnode = wrapChilds.item(j);
                        short ctype = cnode.getNodeType();
                        System.out.println("===:"+cnode.getNodeName()+":"+ctype);

                    }
                }
            }

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Node selectSingleNode(String express, Element source) {
        Node result = null;
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        try {
            result = (Node) xpath.evaluate(express, source, XPathConstants.NODE);
            System.out.println(result);
        } catch (XPathExpressionException e) {
            System.out.println(e.getMessage());
        }
        return result;
    }
    public static NodeList selectNodes(String express, Object source) {//查找节点，返回符合条件的节点集。
        NodeList result=null;
        XPathFactory xpathFactory=XPathFactory.newInstance();
        XPath xpath=xpathFactory.newXPath();
        try {
            result=(NodeList) xpath.evaluate(express, source, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }

        return result;
    }
}
